package org.cms;

import com.atlassian.jira.rest.client.api.domain.Issue;
import org.cms.entities.JiraIssue;
import org.cms.entities.JiraUser;
import org.joda.time.DateTime;

import java.sql.Timestamp;

/**
* Created by elnon on 07.03.15.
*/
public class IssueMapper {

    public static JiraIssue map(Issue issue) {
        JiraIssue jiraIssue = new JiraIssue();

        jiraIssue.setSummary(issue.getSummary());
        jiraIssue.setKey(issue.getKey());
        jiraIssue.setProject(issue.getProject().getName());

        //2015-02-28T16:16:28.000+01:00
        jiraIssue.setCreationDate(convertToTimeStamp(issue.getCreationDate()));
        jiraIssue.setDueDate(convertToTimeStamp(issue.getDueDate()));
        jiraIssue.setUpdateDate(convertToTimeStamp(issue.getUpdateDate()));

        JiraUser repoter = new JiraUser();
        repoter.setDisplayName(jiraIssue.getReporter().getDisplayName());
        repoter.setName(jiraIssue.getReporter().getName());
        jiraIssue.setReporter(repoter);

        JiraUser assignee = new JiraUser();
        assignee.setName(issue.getAssignee().getName());
        assignee.setDisplayName(issue.getAssignee().getDisplayName());
        jiraIssue.setAssignee(assignee);

        return jiraIssue;
    }

    private static Timestamp convertToTimeStamp(DateTime dateTime) {

        return new Timestamp(dateTime.toGregorianCalendar().getTimeInMillis());
    }
}
